//
//  PMGrooveJoint.m
//  PezMonkey
//
//  Created by Spence DiNicolantonio on 10/31/09.
//  Copyright 2009 Necrosoft. All rights reserved.
//

#import "PMGrooveJoint.h"
#import "PMBody.h"


@implementation PMGrooveJoint


#pragma mark -
#pragma mark Init

/**
 * Returns a PMGrooveJoint object initialized by anchoring one given rigid body 
 * to a given groove and another given rigid body to its center of gravity.
 */
- (id)initWithGroove:(PMGroove)groove onBodyA:(PMBody *)aBody BodyB:(PMBody *)otherBody {
	return [self initWithGroove:groove 
						onBodyA:aBody anchor:PMVectZero 
						onBodyB:otherBody];
}

/**
 * Returns a PMGrooveJoint object initialized by anchoring one given rigid body 
 * to a given groove and another given rigid body to a given position reletive to 
 * the body's center of gravity.
 */
- (id)initWithGroove:(PMGroove)groove onBodyA:(PMBody *)aBody anchor:(PMAnchor)anchor onBodyB:(PMBody *)otherBody {
	if (![super init])
		return nil;
	
	// allocate and initialize cpJoint
	joint = cpGrooveJointNew(aBody.cpBody, otherBody.cpBody, groove.start, groove.end, anchor);
	
	// set body references
	self.bodyA = aBody;
	self.bodyB = otherBody;
	
	return self;
}


#pragma mark -
#pragma mark Getters

/**
 * Returns the receiver's groove anchor position for bodyA.
 */
- (PMGroove)groove {
	PMVect start = ((cpGrooveJoint *) joint)->grv_a;
	PMVect end = ((cpGrooveJoint *) joint)->grv_b;
	return PMGrooveMake(start, end);
}

/**
 * Returns the receiver's joint anchor position for bodyA.
 */
- (PMAnchor)anchor {
	return ((cpGrooveJoint *) joint)->anchr2;
}


#pragma mark -
#pragma mark Setters

/**
 * Sets the groove anchor position of the receiver's bodyA to a given groove structure.
 */
- (void)setGroove:(PMGroove)groove {
	((cpGrooveJoint *) joint)->grv_a = groove.start;
	((cpGrooveJoint *) joint)->grv_b = groove.end;
}

/**
 * Sets the joint anchor position of the receiver's bodyB to a given point relative
 * the the body's center of gravity.
 */
- (void)setAnchor:(PMAnchor)anchor {
	((cpGrooveJoint *) joint)->anchr2 = anchor;
}

@end
